<?php namespace App\Http\Services;

class ToolsManager
{
    /**
     * 生成订单编号
     * @return string
     */
    public static function makeBillNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 999));
        return $orderSn;
    }

    public static function getTool($len){
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

    public static function getRandomMoney($money,$size){
        $min = 0.1;

        if((int)$size == 1){
            $money = round($money * 100) / 100;
            $money = $money <= $min ? 1 : $money;
            return $money;
        }

        $max = $money / $size * 2;
        $r   = self::getRandomNum();
        $money = $r * $max;
        $money = round($money * 100) / 100;
        $money = $money <= $min ? 1 : $money;

        return $money;
    }

    public static function getRandomNum(){
        $max = 1;
        $min = 0;
        $b = 0;
        while(true){
            $a = $min + mt_rand()/mt_getrandmax() * ($max-$min);
            $b = round($a,2);
            if($b !== 0)
                break;
        }

        return $b;
    }
}